package com.alvinlkk.basic.chapter21;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class BlockedMutex {
    private Lock lock = new ReentrantLock();
    
    public BlockedMutex() {
        lock.lock();
    }
    
    public void f() {
        try {
            System.out.println("f start----");
            lock.lockInterruptibly();
            System.out.println("lock acquired in f()");
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            System.out.println("Interrupted from lock acquistion in f()");
        }
    }
}

class Blocked2 implements Runnable {

    BlockedMutex blocked = new BlockedMutex();
    
    @Override
    public void run() {
        System.out.println("waiting for f() in blockedMutex()");
        blocked.f();
        System.out.println("Broke out of blocked call");
    }
}

public class Interrupting2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Blocked2());
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Issuing interrupt()");
        thread.interrupt();
    }
}